<template>
  <div class="report-form-content">
    <el-form class="my-form"
             ref="objForm"
             :model="obj"
             :rules="rules"
             label-position="right"
             label-width="auto"
             label-suffix="："
             :disabled="oper === 'view' || oper === 'supplement'">
      <div class="tabs-form-head">
        <span>合同变更</span>
        <div class="">
          <el-button type="primary" plain @click="returnBtn">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确认变更</el-button>
        </div>
      </div>
      <el-divider border-style="dashed" />
      <el-row :gutter="20">
        <el-col :sm="24" :lg="24" style="padding-left: 20px">
          <el-card class="update-log">
            <div class="report-form-list-content">
              <el-form-item label="合同名称" prop="name">
                <el-input v-model="obj.name" placeholder="合同名称" />
              </el-form-item>
              <el-form-item label="签订人" prop="contractSignerId">
                <ContractSigner v-model="obj.contractSignerId" keyName="id" keyShow="name" placeholder="签订人" title="合同签订人查询" />
              </el-form-item>
              <el-form-item label="签订部门" prop="departmentId">
                <Organization v-model="obj.departmentId" keyName="id" keyShow="organizationName" placeholder="签订部门" title="部门查询" />
              </el-form-item>
              <el-form-item label="合同类型" prop="type">
                <el-select v-model="obj.type" placeholder="请选择" clearable filterable>
                  <el-option v-for="item in contractTypeList" :key="item.id" :label="`${item.code} - ${item.name} ${item.state === '0' ? '【已禁用】' : ''}`" :value="item.id" :disabled="item.state === '0'" />
                </el-select>
              </el-form-item>
              <el-form-item label="负责人" prop="userId">
                <User v-model="obj.userId" keyName="id" keyShow="name" placeholder="负责人" title="用户查询" />
              </el-form-item>
              <el-form-item label="合同金额" prop="amount">
                <el-input-number v-model="obj.amount" placeholder="请输入合同金额" />
              </el-form-item>
              <el-form-item label="合同起始时间" prop="contractLaunch">
                <el-date-picker v-model="obj.contractLaunch" type="date" placeholder="选择日期" value-format="YYYY-MM-DD" />
              </el-form-item>
              <el-form-item label="合同终止时间" prop="contractComplete">
                <el-date-picker v-model="obj.contractComplete" type="date" placeholder="选择日期" value-format="YYYY-MM-DD" />
              </el-form-item>
            </div>
            <div class="report-form-list-content-1">
              <el-form-item label="合同内容" prop="content">
                <editor v-model="obj.content" v-model:readOnly="readOnly" :height="800" />
              </el-form-item>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-form>
    <selectApprove v-model:itemsVisible="dialogApproveVisible" @close="dialogApproveVisible = false" :business="'3004'" :businessId="obj.id" @doSubmit="doSubmit" />
  </div>
</template>
<script setup>
import {getCurrentInstance, ref} from 'vue'
import {doGet, doPost} from "@/config/http";
import {svc, getListPageSize, alertErrorMsg, notifySuccess} from "@/config/common";
import {useRoute} from 'vue-router'
import {getUserId} from "@/config/cookie";
import useUserStore from "@/store/modules/user";
import selectApprove from "@/views/components/base/selectApprove.vue";
import User from "@/views/components/base/selectUser.vue";
import Organization from "@/views/components/base/selectOrganization.vue";
import ContractSigner from "@/views/components/contract/selectContractSigner.vue";
import initListStore from '@/store/modules/initList'

const route = useRoute()

const { proxy: ctx } = getCurrentInstance();

let objForm = ref(null)
let dialogApproveVisible = ref(false)
const oper = ref(route.query.oper)
const readOnly = ref(false)

let obj = ref({
  contractInitialId: route.query.id, // 合同ID
  batch: null, // 批次号
  changeUserId: null, // 变更经办人
  changeDepartmentId: null, // 变更经办人部门
  changeReason: null, // 变更原因
  code: null, // 合同编码
  name: null, // 合同名称
  type: null, // 合同类型
  contractSignerId: null, // 合同签订人ID
  contractSignerDepartmentId: null, // 合同签订部门ID
  departmentId: null, // 负责部门ID
  userId: null, // 负责人用户ID
  amount: null, // 合同金额
  contractLaunch: null, // 合同起始时间
  contractComplete: null, // 合同终止时间
  content: null, // 合同内容
  id: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
  deleted: null,
  version: null,
})

// let objList = ref([])

let contractTypeList = initListStore().contractTypeList // 合同类型
// let contractCooperationList = ref([]) // 往来单位
// let contractFeetypeList = ref([]) // 合同费用类型
// let contractTemplateList = ref([]) // 合同模版
// let bankList = ref([]) // 银行
// let userList = ref([]) // 用户
// let governmentList = ref([]) //  政府单位
// let organizationList = ref([]) // 部门
// let contractSignerList = ref([]) // 签订人
// let contractFiletypeList = ref([]) // 合同附件类型

const rules = {}

// const initList = async () => {
//   let p1 = doGet(svc, '/contract-type/page', {pageSize: getListPageSize()}, false) // 合同类型
//   let p2 = doGet(svc, '/contract-cooperation/page', {pageSize: getListPageSize()}, false) // 往来单位
//   let p3 = doGet(svc, '/contract-feetype/page', {pageSize: getListPageSize()}, false) // 合同费用类型
//   let p4 = doGet(svc, '/contract-template/page', {pageSize: getListPageSize()}, false) // 合同模版
//   let p5 = doGet(svc, '/bank/page', {pageSize: getListPageSize()}, false) // 银行
//   let p6 = doGet(svc, '/user/page', {pageSize: getListPageSize()}, false) // 用户
//   let p7 = doGet(svc, '/government/page', {pageSize: getListPageSize()}, false) //  政府单位
//   let p8 = doGet(svc, '/organization/page', {pageSize: getListPageSize()}, false) // 部门
//   let p9 = doGet(svc, '/contract-signer/page', {pageSize: getListPageSize()}, false) // 签订人
//   let p10 = doGet(svc, '/contract-filetype/page', {pageSize: getListPageSize()}, false) // 合同附件类型

//   let resArray = await Promise.all([p1, p2, p3, p4, p5, p6, p7, p8, p9, p10])
//   contractTypeList.value = resArray[0]['records']
//   contractCooperationList.value = resArray[1]['records']
//   contractFeetypeList.value = resArray[2]['records']
//   contractTemplateList.value = resArray[3]['records']
//   bankList.value = resArray[4]['records']
//   userList.value = resArray[5]['records']
//   governmentList.value = resArray[6]['records']
//   organizationList.value = resArray[7]['records']
//   contractSignerList.value = resArray[8]['records']
//   contractFiletypeList.value = resArray[9]['records']
// }
const initObj = async () =>  {
  if (route.query.id) {
    let obj1 = await doGet(svc, '/contract-initial/' + route.query.id, {}, true)
    console.log('obj1------------',obj1)
    obj.value.code = obj1['code']
    obj.value.name = obj1['name']
    obj.value.contractSignerId = obj1['contractSignerId']
    obj.value.departmentId = obj1['departmentId']
    obj.value.type = obj1['type']
    obj.value.userId = obj1['userId']
    obj.value.amount = obj1['amount']
    obj.value.contractLaunch = obj1['contractLaunch']
    obj.value.contractComplete = obj1['contractComplete']
    obj.value.content = obj1['content']
  }
}
initObj()
// initList()

const handleSubmit = async () => {
  let v = await objForm.value.validate().catch((err)=>{return err})
  if (v !== true) {
    alertErrorMsg('表单验证不通过，请检查数据。')
    return
  }
  dialogApproveVisible.value = true
}

const doSubmit = async (data) => {
  let v = await objForm.value.validate().catch((err)=>{return err})
  if (v !== true) {
    alertErrorMsg('表单验证不通过，请检查数据。')
    return
  }
  obj.value.changeUserId = getUserId()
  obj.value.changeDepartmentId = useUserStore().organization.id
  let o = {}
  o.oper = 'add'
  o.approveId = data.approveId
  o.approveNodes = data.approveNodes
  o.items = [obj.value]
  doPost(svc, `/customContractInitial/changeContractInitial/${route.query.id}`, o, true).then(() => {
    notifySuccess('操作成功')
    returnBtn()
  })
}

const returnBtn = () => {
  switch (route.query.kind) {
    case '1':
      ctx.goTargetByName('contractInitial', {});
      break;
    case '2':
      ctx.goTargetByName('contractMain', {})
      break;
  }
}
</script>
<style scoped>
.report-form-content {
  padding: 20px;
}
.update-log {
  width: 100%;
  margin: 30px 0;
}
.report-list span:nth-child(1) {
  display: inline-block;
  width: 120px;
  text-align: right;
}
.tabs-form-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.tabs-form-head span {
  font-size: 18px;
  color: #303133;
  font-weight: bold;
}
</style>
